﻿@model CustomerAddressEditModel
@using SpotsHub.Gateway.Models.Customer;
@{
    Layout = "~/Views/Shared/_MyHUB.cshtml";

    //title
    Html.AddTitleParts(T("PageTitle.Account").Text);
}

@using (Html.BeginForm())
{
    <div class="page account-page address-edit-page customer-info-page">

        <div class="window">

            <div class="window-title grad-lightgrey grad">
                <div class="window-title-bar grad-titleshine grad">
                    <h2 class="page-title">@T("Account.CustomerAddresses.Edit")</h2>
                    @Html.HiddenFor(x => x.ShipmentId, new { id = "shipmentId" })
                </div>
            </div>
            <div class="window-content space">
                <div class="window-title-bar grad-titleshine grad">
                    Change to an existing address:
                </div>
                <div class="window-content space">
                    <div class="buttons">
                        @Html.DropDownListFor(model => model.Address.Id, (IList<SelectListItem>)Model.CustomProperties["addresslist"])
                        @*<input type="submit" name="selectaddress" value="Select" class="button-red" />*@
                    </div>
                </div>
            </div>
            <div class="window-content space">

                @{
    var dataDictAddress = new ViewDataDictionary();
    //Merge ModelState (required for validation)
    dataDictAddress.ModelState.Merge(ViewData.ModelState);
    dataDictAddress.TemplateInfo.HtmlFieldPrefix = "Address";

                }


                @Html.ValidationSummary(false)
                @Html.HiddenFor(model => Model.Address.Id)
                <div class="window-left">
                    <div class="info-row">
                        <div class="info-row-left">
                            <div class="info-label">
                                @Html.RequiredHint()
                                @Html.LabelFor(model => Model.Address.FirstName)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.FirstName, new { id = "FirstName-" + Model.Address.Id.ToString() })
                            </div>
                        </div>
                        <div class="info-row-right">
                            <div class="info-label">
                                @Html.RequiredHint()
                                @Html.LabelFor(model => Model.Address.LastName)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.LastName, new { id = "LastName-" + Model.Address.Id.ToString() })
                            </div>
                        </div>
                    </div>
                    @if (Model.Address.CompanyEnabled)
                    {
                        <div class="info-row">
                            <div class="info-label">
                                @if (Model.Address.CompanyRequired)
                                {
                                    @Html.RequiredHint()
                                }
                                @Html.LabelFor(model => Model.Address.Company)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.Company, new { id = "Company-" + Model.Address.Id.ToString() })
                            </div>
                        </div>
                    }
                    <div class="info-row">
                        <div class="info-label">
                            @Html.RequiredHint()
                            @Html.LabelFor(model => Model.Address.Email)
                        </div>
                        <div class="info-input">
                            @Html.TextBoxFor(model => Model.Address.Email, new { id = "Email-" + Model.Address.Id.ToString() })
                        </div>
                    </div>
                    @if (Model.Address.PhoneEnabled)
                    {
                        <div class="info-row">
                            <div class="info-row-left">
                                <div class="info-label">
                                    @if (Model.Address.CellPhoneRequired)
                                    {
                                        @Html.RequiredHint()
                                    }
                                    @Html.LabelFor(model => Model.Address.CellPhoneNumber)
                                </div>
                                <div class="info-input">
                                    @Html.TextBoxFor(model => Model.Address.CellPhoneNumber, new { id = "CellPhoneNumber-" + Model.Address.Id.ToString() })
                                </div>
                            </div>
                            <div class="info-row-right">
                                <div class="info-label">
                                    @Html.LabelFor(model => Model.Address.PhoneNumber)
                                </div>
                                <div class="info-input">
                                    @Html.TextBoxFor(model => Model.Address.PhoneNumber, new { id = "PhoneNumber-" + Model.Address.Id.ToString() })
                                </div>
                            </div>
                        </div>
                    }

                </div>
                <div class="window-right">
                    @if (Model.Address.StreetAddressEnabled)
                    {
                        <div class="info-row">
                            <div class="info-label">
                                @if (Model.Address.StreetAddressRequired)
                                {
                                    @Html.RequiredHint()
                                }
                                @Html.LabelFor(model => Model.Address.Address1)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.Address1, new { id = "Address1-" + Model.Address.Id.ToString() })
                            </div>
                        </div>
                    }
                    @if (Model.Address.StreetAddress2Enabled)
                    {
                        <div class="info-row">
                            <div class="info-label">
                                @Html.LabelFor(model => Model.Address.Address2)
                            </div>
                            <div class="info-input">
                                @Html.TextBoxFor(model => Model.Address.Address2, new { id = "Address2-" + Model.Address.Id.ToString() })
                            </div>
                        </div>
                    }

                    @if (Model.Address.ZipPostalCodeEnabled || Model.Address.CityEnabled)
                    {
                        <div class="info-row">
                            @if (Model.Address.ZipPostalCodeEnabled)
                            {
                                <div class="info-row-left">
                                    <div class="info-label">
                                        @if (Model.Address.ZipPostalCodeRequired)
                                        {
                                            @Html.RequiredHint()
                                        }
                                        @Html.LabelFor(model => Model.Address.ZipPostalCode)
                                    </div>
                                    <div class="info-input">
                                        @Html.EditorFor(model => Model.Address.ZipPostalCode, new { id = "ZipPostalCode-" + Model.Address.Id.ToString() })
                                    </div>
                                    @Html.ValidationMessageFor(model => Model.Address.ZipPostalCode)
                                </div>
                            }
                            @if (Model.Address.CityEnabled)
                            {
                                <div class="info-row-right">
                                    <div class="info-label">
                                        @if (Model.Address.CityRequired)
                                        {
                                            @Html.RequiredHint()
                                        }
                                        @Html.LabelFor(model => Model.Address.City)
                                    </div>
                                    <div class="info-input">
                                        @Html.EditorFor(model => Model.Address.City, new { id = "City-" + Model.Address.Id.ToString() })
                                    </div>
                                    @Html.ValidationMessageFor(model => Model.Address.City)
                                </div>

                            }
                        </div>
                    }
                    @if (Model.Address.CountryEnabled && Model.Address.StateProvinceEnabled)
                    {
                        <div class="info-row">
                            @if (Model.Address.CountryEnabled)
                            {
                                <div class="info-row-left">
                                    <div class="info-label">
                                        @Html.LabelFor(model => Model.Address.CountryId)
                                    </div>
                                    <div class="info-input">
                                        @Html.DropDownListFor(model => Model.Address.CountryId, Model.Address.AvailableCountries, new { id = "CountryId-" + Model.Address.Id.ToString() })
                                    </div>
                                </div>

                            }
                            @if (Model.Address.CountryEnabled && Model.Address.StateProvinceEnabled)
                            {
                                <div class="info-row-right">
                                    <div class="info-label">
                                        @Html.LabelFor(model => Model.Address.StateProvinceId)
                                    </div>
                                    <div class="info-input">
                                        @Html.DropDownListFor(model => Model.Address.StateProvinceId, Model.Address.AvailableStates, new { id = "StateProvinceId-" + Model.Address.Id.ToString() })
                                    </div>
                                </div>
                            }
                        </div>

                    }
                </div>
            </div>
            <div class="content-clear"></div>

            <div class="window-content space">
                <div class="buttons">
                    <input type="submit" class="button-red save-address-button" name="selectaddress" value="@T("Common.Save")" />
                </div>
            </div>
            <script type="text/javascript">
                //$("select[id*=CountryId-]").live("change", function () {
                $("select").change(function () {
                    var selectedItem = $(this).val();
                    var ddlStates = $(this).closest('form').find('[id*=StateProvinceId-]');

                    $.ajax({
                        type: "GET",
                        url: '@Url.Action("AddressSelect", "Customer")',
                        data: { addressId: selectedItem},
                        dataType: "json",
                        success: function (returndata) {
                            if (returndata.Success == true) {
                                $('#FirstName-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.FirstName);
                                $('#LastName-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.LastName);
                                $('#Company-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.Company);
                                $('#Email-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.Email);
                                $('#CellPhoneNumber-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.CellPhoneNumber);
                                $('#PhoneNumber-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.PhoneNumber);
                                $('#Address1-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.Address1);
                                $('#Address2-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.Address2);
                                $('#Address_ZipPostalCode').val(returndata.objModel.Address.ZipPostalCode);
                                $('#Address_City').val(returndata.objModel.Address.City);
                                $('CountryId-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.CountryId);
                                $('StateProvinceId-' + @Model.Address.Id.ToString()).val(returndata.objModel.Address.StateProvinceId);
                            }
                            if (returndata.Success == false) {
                                window.location.href = "@Url.RouteUrl("CustomerAddresses")";
                            }
                        }
                    });
                    @*$.ajax({
                            cache: false,
                            type: "GET",
                            url: "@(Url.RouteUrl("GetStatesByCountryId"))",
                            data: { "countryId": selectedItem, "addEmptyStateIfRequired": "true" },
                            success: function (data) {
                                ddlStates.html('');insertAfter(ddlStates.closest('.beautify-select')).removeClass("beautified").prev('.beautify-select').remove();
                                }
                                $.each(data, function (id, option) {
                                    ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                });
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert('Failed to retrieve states.');
                            }
                        });*@
                });
            </script>
        </div>
    </div>
}